﻿using NewsCollection.Data;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using NewsCollection.ListWeb;
using NewsCollection.Common.UpdateGUI;
using NewsCollection.Common.Service;
using System.Text.RegularExpressions;
using HtmlAgilityPack;
using System.Net;
using System.Data.SqlClient;
using System.IO;

namespace NewsCollection
{
    public partial class Form1 : Form
    {
        ProcessDb pdb = new ProcessDb();
        public Form1()
        {
            InitializeComponent();
            //    var bi= Extensions.ImageToByteArrayFromFilePath("e://demo.png");
            //   Extensions.ByteArrayToImage(bi).SaveImageToFolder("e://demo//demo.png");
          //  string s = "04:45 ICT Thứ tư, ngày 23 tháng tư năm 2014";
          //   string s = "H236nh ảnh mới di234m d250a của 39c244ng ch250a tự phong39 L226m Chi Khanh";
            //  s.Replace(" ", "").Trim();
           //int i= s.IndexOf("ngày");
     //      int j= s.IndexOf("tháng");
          
        //   MessageBox.Show(s.FixErrorText());
        }
        Thread thread;
        UpdateButton updateBtn = new UpdateButton();
        UpdateTreeView updateTree = new UpdateTreeView();
        UpdateCombobox updateCbb = new UpdateCombobox();
        DataContext db = new DataContext("name=news");
        SqlConnection cn = new SqlConnection
            (@"Data Source=sonic;Initial Catalog=dbNewsCollection;Integrated Security=True");

      
        SqlCommand cmd = new SqlCommand();
        SqlDataReader dr;
        private void btnStart_Click(object sender, EventArgs e)
        {

            int manuId = Convert.ToInt32(comboBox1.SelectedValue);
            if (btnStart.Text.Equals("Start"))
            {
                listView1.Items.Clear();
                updateBtn.UpdateClickStart(btnStart, btnStop);

                //  this.listView1.Items.Clear();


                this.listView1.View = View.Details;
                this.listView1.FullRowSelect = true;
                string folder = "E:/Website/WebsiteTinTucThoiTrang/WebsiteTinTucThoiTrang/Content/images/thumbs";
                if (!Directory.Exists(folder + "/Normal/News"))
                    Directory.CreateDirectory(folder + "/Normal/News");
                if (!Directory.Exists(folder + "/Normal/Featured"))
                    Directory.CreateDirectory(folder + "/Normal/Featured");

                if (!Directory.Exists(folder + "/700x256/Featured"))
                    Directory.CreateDirectory(folder + "/700x256/Featured");
                if (!Directory.Exists(folder + "/700x256/News"))
                    Directory.CreateDirectory(folder + "/700x256/News");

                if (!Directory.Exists(folder + "/200x130/Featured"))
                    Directory.CreateDirectory(folder + "/200x130/Featured");
                if (!Directory.Exists(folder + "/200x130/News"))
                    Directory.CreateDirectory(folder + "/200x130/News");

                if (!Directory.Exists(folder + "/315x240/Featured"))
                    Directory.CreateDirectory(folder + "/315x240/Featured");
                if (!Directory.Exists(folder + "/315x240/News"))
                    Directory.CreateDirectory(folder + "/315x240/News");

                Afamilyvn afamilyvn = new Afamilyvn();
                ThreadStart starter;
                starter = () => afamilyvn.Run(listView1, btnStart, btnStop, 1, folder);
                thread = new Thread(starter);
                thread.Start();

                DepComVn dep = new DepComVn();
                ThreadStart starter1;
                starter1 = () => dep.Run(listView1, btnStart, btnStop, 2, folder);
                thread = new Thread(starter1);
                thread.Start();

                Kenh14vn kenh14 = new Kenh14vn();
                ThreadStart starter2;
                starter2 = () => kenh14.Run(listView1, btnStart, btnStop, 3, folder);
                thread = new Thread(starter2);
                thread.Start();

                NewsZing newszing = new NewsZing();
                ThreadStart starter3;
                starter3 = () => newszing.Run(listView1, btnStart, btnStop, 4, folder);
                thread = new Thread(starter3);
                thread.Start();


                YanVn yanvn = new YanVn();
                ThreadStart starter4;
                starter4 = () => yanvn.Run(listView1, btnStart, btnStop, 5, folder);
                thread = new Thread(starter4);
                thread.Start();

                NgoisaoVn ngoisao = new NgoisaoVn();
                ThreadStart starter5;
                starter5 = () => ngoisao.Run(listView1, btnStart, btnStop, 6, folder);
                thread = new Thread(starter5);
                thread.Start();

                _2SaoVn _2sao = new _2SaoVn();
                ThreadStart starter6;
                starter6 = () => _2sao.Run(listView1, btnStart, btnStop, 7, folder);
                thread = new Thread(starter6);
                thread.Start();

                ElleVn ellevn = new ElleVn();
                ThreadStart starter7;
                starter7 = () => ellevn.Run(listView1, btnStart, btnStop, 8, folder);
                thread = new Thread(starter7);
                thread.Start();

                        //Yahoo yahoo = new Yahoo();
                        //ThreadStart starter8;
                        //starter8 = () => yahoo.Run(listView1, btnStart, btnStop, 9, folder);
                        //thread = new Thread(starter8);
                        //thread.Start();

                //DanTri dantri = new DanTri();
                //ThreadStart starter9;
                //starter9 = () => dantri.Run(listView1, btnStart, btnStop, 9, folder);
                //thread = new Thread(starter9);
                //thread.Start();

                GiaDinhNet giadinh = new GiaDinhNet();
                ThreadStart starter10;
                starter10 = () => giadinh.Run(listView1, btnStart, btnStop, 10, folder);
                thread = new Thread(starter10);
                thread.Start();

                TinMoiVn tinmoi = new TinMoiVn();
                ThreadStart starter11;
                starter11 = () => tinmoi.Run(listView1, btnStart, btnStop, 11, folder);
                thread = new Thread(starter11);
                thread.Start();
                    
                

                //switch (comboBox1.SelectedIndex)
                //{
                //    case 0:
                //        Afamilyvn afamilyvn = new Afamilyvn();
                //        ThreadStart starter;
                //        starter = () => afamilyvn.Run(listView1, btnStart, btnStop, manuId);
                //        thread = new Thread(starter);
                //        thread.Start();
                //        break;
                //    case 1:
                //        DepComVn dep = new DepComVn();
                //        ThreadStart starter1;
                //        starter1 = () => dep.Run(listView1, btnStart, btnStop, manuId);
                //        thread = new Thread(starter1);
                //        thread.Start();
                //        break;
                //    case 2:
                //        Kenh14vn kenh14 = new Kenh14vn();
                //        ThreadStart starter2;
                //        starter2 = () => kenh14.Run(listView1, btnStart, btnStop, manuId);
                //        thread = new Thread(starter2);
                //        thread.Start();
                //        break;
                //    case 3:
                //        NewsZing newszing = new NewsZing();
                //        ThreadStart starter3;
                //        starter3 = () => newszing.Run(listView1, btnStart, btnStop, manuId);
                //        thread = new Thread(starter3);
                //        thread.Start();
                //        break;
                //    case 4:
                      

                //        YanVn yanvn = new YanVn();
                //        ThreadStart starter4;
                //        starter4 = () => yanvn.Run(listView1, btnStart, btnStop, manuId);
                //        thread = new Thread(starter4);
                //        thread.Start();
                //        break;
                //    case 5:
                //        NgoisaoVn ngoisao = new NgoisaoVn();
                //        ThreadStart starter5;
                //        starter5 = () => ngoisao.Run(listView1, btnStart, btnStop, manuId);
                //        thread = new Thread(starter5);
                //        thread.Start();
                //        break;
                //    case 6:
                //        _2SaoVn _2sao = new _2SaoVn();
                //        ThreadStart starter6;
                //        starter6 = () => _2sao.Run(listView1, btnStart, btnStop, manuId);
                //        thread = new Thread(starter6);
                //        thread.Start();
                //        break;
                //    case 7:
                //        ElleVn ellevn = new ElleVn();
                //        ThreadStart starter7;
                //        starter7 = () => ellevn.Run(listView1, btnStart, btnStop, manuId);
                //        thread = new Thread(starter7);
                //        thread.Start();
                //        break;
                //    case 8:
                //        Yahoo yahoo = new Yahoo();
                //        ThreadStart starter8;
                //        starter8 = () => yahoo.Run(listView1, btnStart, btnStop, manuId);
                //        thread = new Thread(starter8);
                //        thread.Start();
                //        break;
                //    case 9:
                //        DanTri dantri = new DanTri();
                //        ThreadStart starter9;
                //        starter9 = () => dantri.Run(listView1, btnStart, btnStop, manuId);
                //        thread = new Thread(starter9);
                //        thread.Start();
                //        break;
                //    case 10:
                //        GiaDinhNet giadinh = new GiaDinhNet();
                //        ThreadStart starter10;
                //        starter10 = () => giadinh.Run(listView1, btnStart, btnStop, manuId);
                //        thread = new Thread(starter10);
                //        thread.Start();
                //        break;
                //    case 11:
                //        TinMoiVn tinmoi = new TinMoiVn();
                //        ThreadStart starter11;
                //        starter11 = () => tinmoi.Run(listView1, btnStart, btnStop, manuId);
                //        thread = new Thread(starter11);
                //        thread.Start();
                //        break;
                //    default:
                //        Console.WriteLine("Default case");
                //        break;
                //}
                // ThreadStart 



            }
            else if (btnStart.Text.Equals("Pause"))
            {
                updateBtn.UpdateClickPause(btnStart, btnStop, thread);
            }
            else if (btnStart.Text.Equals("Continue"))
            {
                updateBtn.UpdateClickContinous(btnStart, btnStop, thread);
            }


        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            listView1.Items.Clear();
            updateBtn.UpdateClickStop(btnStart, btnStop, thread);
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            DataManual categorymanual = new DataManual();
            categorymanual.AddToManu();
            var list = db.Manufactory.ToList();
            comboBox1.DataSource = list;
            comboBox1.DisplayMember = "Url";
            comboBox1.ValueMember = "Id";

            // add to manual
       
            categorymanual.AddToCategoryManual();
            //CategoryManuals cama1 = new CategoryManuals();
            //CategoryManuals cama2 = new CategoryManuals();
            //CategoryManuals cama3 = new CategoryManuals();
            //CategoryManuals cama4 = new CategoryManuals();
            //CategoryManuals cama5 = new CategoryManuals();
            //CategoryManuals cama6 = new CategoryManuals();
            //CategoryManuals cama7 = new CategoryManuals();
            //CategoryManuals cama8 = new CategoryManuals();
            //CategoryManuals cama9 = new CategoryManuals();
            //CategoryManuals cama10 = new CategoryManuals();
            //CategoryManuals cama11 = new CategoryManuals();
            //CategoryManuals cama12 = new CategoryManuals();
            //CategoryManuals cama13 = new CategoryManuals();
            //List<CategoryManuals> CategoryManuals = new List<CategoryManuals>();
            //cama1.MyId = "1";
            //cama1.MyName = "Thoitrang";
            //CategoryManuals.Add(pdb.SaveCategoryManual(cama1));
            //cama2.MyId = "2";
            //cama2.MyName = "Sao";
            //CategoryManuals.Add(pdb.SaveCategoryManual(cama2));
            //cama3.MyId = "3";
            //cama3.MyName = "Teen";
            //CategoryManuals.Add(pdb.SaveCategoryManual(cama3));
            //cama4.MyId = "4";
            //cama4.MyName = "Lamdep";
            //CategoryManuals.Add(pdb.SaveCategoryManual(cama4));
            //cama5.MyId = "5";
            //cama5.MyName = "Hinh";
            //CategoryManuals.Add(pdb.SaveCategoryManual(cama5));
            //cama6.MyId = "6";
            //cama6.MyName = "Giaitri";
            //CategoryManuals.Add(pdb.SaveCategoryManual(cama6));
            //cama7.MyId = "7";
            //cama7.MyName = "Video";
            //CategoryManuals.Add(pdb.SaveCategoryManual(cama7));

            //cama8.MyId = "8";
            //cama8.MyName = "Tinthoitrang";
            //cama8.ParentCategoryManual = cama1;

            //cama8.MyId = "8";
            //cama8.MyName = "Tinthoitrang";
            //cama8.ParentCategoryManual = cama1;

            //cama8.MyId = "8";
            //cama8.MyName = "Tinthoitrang";
            //cama8.ParentCategoryManual = cama1;

            //cama8.MyId = "8";
            //cama8.MyName = "Tinthoitrang";
            //cama8.ParentCategoryManual = cama1;

            //CategoryManuals.Add(pdb.SaveCategoryManual(cama8));

            updateTree.UpdateCateManual(treeView1);
            updateCbb.UpdateListCategory(cbbListCate);

            //string s = "\r\n                    Thời trang\r\n                    -  09/04/2014 08:21";
            //MessageBox.Show(s.GetDateInString());
        }

        private void btnBrowerImg_Click(object sender, EventArgs e)
        {
            var dialog = new FolderBrowserDialog();
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                txtFolder.Text = dialog.SelectedPath;
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
           
            //ThreadStart starter;
            //starter = () => pdb.SaveImgPc(txtFolder.Text);
            //Thread thread = new Thread(starter);
            //thread.Start();

         //   MessageBox.Show("Lưu hình vào folder thành công!");


        }

        private void tagSelectedIndexChanged(object sender, EventArgs e)
        {
            if (tabControl1.SelectedIndex == 1)
            {
                //  MessageBox.Show("abc");
            //    updateTree.UpdateCateManual(treeView1);
                updateTree.UpdateNode(treeView2);
             
            }


        }

        private void button1_Click(object sender, EventArgs e)
        {
            TreeNode node = new TreeNode();
            node = treeView2.SelectedNode;

            treeView2.SelectedNode.Remove();
            TreeNode nodeManual = new TreeNode();
            nodeManual = treeView1.SelectedNode;
            nodeManual.Nodes.Add(node);

        }

        private void Save_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < treeView1.Nodes.Count; i++)
                pdb.RecursiveCategoryManual(treeView1.Nodes[i]);
            //var listCateManual = db.CategoryManual.ToList();
          
         
        
            //for (int i = 0; i < treeView1.Nodes.Count; i++)
            //{
            //    var node = treeView1.Nodes[i];
            //    var cateManual = pdb.SearchCategoryManuals(node.Name, node.Text);
            //    var listCateMappingManual = db.Categories.Where(m => m.CategoryManual != null).ToList();
            //    if(listCateMappingManual.Count >0)
            //    listCateMappingManual.Where(m => m.CategoryManual == cateManual).ToList();
            //    //so sánh nếu như nodes trong treeview >  listCate co manual  thì có thay đổi xử lý
            //    if (treeView1.Nodes[i].Nodes.Count > listCateMappingManual.Count && treeView1.Nodes[i].Nodes.Count >0)
            //    {
            //        //lay danh sach cate chua mapping voi manual
            //        var listCateNoMappingManual = db.Categories.Where(m => m.CategoryManual == null).ToList();
            //        //if (listCateNoMappingManual.Count > 0)
            //        //    listCateNoMappingManual.Where(m => m.ParentCategory.sId.Equals(treeView1.Nodes[i].Text) &&
            //        //        m.ParentCategory.Id.Equals(treeView1.Nodes[i].Name)).ToList();
            //       // int dem = 0;
                  
            //        for (int j = 0; j < listCateNoMappingManual.Count; j++)
            //        {
            //            for (int h = 0; h < treeView1.Nodes[i].Nodes.Count; h++)
            //            {
            //                if (treeView1.Nodes[i].Nodes[h].Text.Equals(listCateNoMappingManual[j].sId) &&

            //              treeView1.Nodes[i].Nodes[h].Name.Equals(listCateNoMappingManual[j].Id+""))
            //                {
            //                    //neu co  phan tu duoc them
            //                    var parentNode = treeView1.Nodes[i].Nodes[h].Parent;
            //                    var parent = pdb.SearchCategoryManuals(parentNode.Name, parentNode.Text);
            //                    if (parent != null )
            //                    {
            //                        //parent.Categories.Add(
                                    
            //                       // listCateNoMappingManual[j].CategoryManual = parent;
            //                        cn.Open();
            //                        cmd.CommandText = "Update Categories set CategoryManual_Id='"+parent.Id+"' where id='" + listCateNoMappingManual[j].Id + "'";
            //                        //  cmd.CommandText = "select * from Categories";
            //                        // SqlCommand comand = new SqlCommand(cmd, cn);
            //                        cmd.Connection = cn;
            //                        cmd.ExecuteNonQuery();
            //                        cn.Close();

            //                      //  db.SaveChanges();
                                  
            //                    }

            //                }
            //            }
                      
            //            //dem++;
            //            //if (dem == treeView1.Nodes[i].Nodes.Count - listCateMappingManual.Count)
            //            //    break;
                      
            //        }
            //    }

             
              
            //}
            MessageBox.Show("Lưu thành công!");
            //if(treeView1.Nodes.Count > listCate.Count+ listCateManual.Count)
            //{
                  
               
            //}
           
        }

        private void treeView2_DrawNode(object sender, DrawTreeNodeEventArgs e)
        {

            if (e.Node.Checked)
            {
                Font nodeFont = e.Node.NodeFont;
                nodeFont = new Font(nodeFont, FontStyle.Bold);
                e.Graphics.FillRectangle(Brushes.Bisque, e.Node.Bounds);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("Bạn có thật sự muốn xoá Subcategory này?", "",MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
           
            switch (result)
            {
                case DialogResult.OK:
                    //   var item = pdb(treeView1.SelectedNode.Name, treeView1.SelectedNode.Text);
                    TreeNode node = new TreeNode();
                    node = treeView1.SelectedNode;

                   var parentCateManual= pdb.SearchCategoryManuals(node.Text);
                   if (parentCateManual != null)
                   {
                       MessageBox.Show("Đây là CategoryManual bạn không được Tree View Auto!");
                   }
                   else
                   {
                       Category item = pdb.SearchCategory(Convert.ToInt32(node.Name));

                       if (item != null)
                       {
                           cn.Open();
                           cmd.CommandText = "  Update Categories set CategoryManual_Id=null where id='" + item.Id + "'";
                           //  cmd.CommandText = "select * from Categories";
                           // SqlCommand comand = new SqlCommand(cmd, cn);
                           cmd.Connection = cn;
                           cmd.ExecuteNonQuery();
                           cn.Close();

                       }
                       treeView1.Nodes.Remove(node);
                       updateTree.UpdateNode(treeView2);
                   }
                   
                    break;
                case DialogResult.Cancel:

                    break;

            }
        }

        private void btnSaveCateManual_Click(object sender, EventArgs e)
        {
             CategoryManuals cama = new CategoryManuals();
            cama.MyId = txtIdManual.Text.Trim() ;
            cama.MyName = txtNameManual.Text.Trim();
            CategoryManuals item = null;
            if (checkBox1.Checked == true)
            {
                item = pdb.CheckCategoryManual(Convert.ToInt32(cbbListCate.SelectedValue));
            }
          

            cama.ParentCategoryManual = item;
           //var item= comboBox1.SelectedItem;
           // var item2=comboBox1.SelectedValue;

            if (pdb.SaveCategoryManual(cama) == null)
            {
                MessageBox.Show("Loại này đã tồn tại!");
            }
            else
            {
               
                    //cn.Open();
                    //cmd.CommandText = "Update CategoryManuals set ParentCategoryManual_Id='" +1 +
                    //    "' where id='" + pdb.SaveCategoryManual(cama).Id + "'";
                    ////  cmd.CommandText = "select * from Categories";
                    //// SqlCommand comand = new SqlCommand(cmd, cn);
                    //cmd.Connection = cn;
                    //cmd.ExecuteNonQuery();
                    //cn.Close();
                
                   
             ///   }
                updateTree.UpdateCateManual(treeView1);
                updateCbb.UpdateListCategory(cbbListCate);
                MessageBox.Show("Thành công");
            }
          
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked == true)
            {
                cbbListCate.Enabled = true;
             //   updateTree.UpdateCateManual(treeView1);
                updateCbb.UpdateListCategory(cbbListCate);
            }
            //if (CheckState.Checked.Equals(1))
            //{

            //}

        }

      


    }
}
